package com.situ.feemanagement.config;

import com.situ.feemanagement.converter.String2DateConverter;
import com.situ.feemanagement.interceptor.LoginInterceptor;
import com.situ.feemanagement.pojo.User;
import com.situ.feemanagement.service.IAuthService;
import com.situ.feemanagement.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private IUserService userService;
    @Autowired
    private IAuthService authService;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**").addResourceLocations("file:/D:/mypic/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor(userService, authService))
                .addPathPatterns("/**")
                .excludePathPatterns("/page/login", "/user/login", "/user/logout", "/number/code", "/static/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new String2DateConverter());
    }
}
